package android.longs.study.mytest.service;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import org.hibernate.Session;

import android.longs.study.base.ResponeModel;
import android.longs.study.mytest.dao.CommonSubDao;
import android.longs.study.mytest.dao.CommonSubDaoImple;
import android.longs.study.mytest.model.CommonItemSubjectModel;
import android.longs.study.mytest.model.CommonUserSubjectModel;
import android.longs.study.user.dao.user.UserDao;
import android.longs.study.user.dao.user.UserDaoImpl;
import android.longs.study.user.model.sub.UserItemSubjectModel;
import android.longs.study.user.model.sub.UserSubjectModel;
import android.longs.study.user.model.user.UserModel;
import android.longs.study.utils.DataUtils;
import android.longs.study.utils.GsonUtils;
import android.longs.study.utils.H3Utils;

import com.alibaba.fastjson.JSON;

public class MyTestListService {

    private CommonSubDao dao = new CommonSubDaoImple();
    private UserDao userDao = new UserDaoImpl();
    //获取所有的测试题列表 
    //获取所有的测试题列表 
    public String getAllTestList(HttpServletRequest request) {

	String index = request.getParameter("index");

	ResponeModel<List<CommonUserSubjectModel>> responeModel = new ResponeModel<>();


	if (index==null||index.equals("")) {
	    index = "0";
	} 
	int start = 0;
	System.out.println(start);
	try {
	    start = Integer.valueOf(index);
	} catch (Exception e) {
	    start = 0;
	}
	try {
	    List<CommonUserSubjectModel> queryAllTestModels = dao.queryAllTestModels(start);
	    
	    for (CommonUserSubjectModel commonUserSubjectModel : queryAllTestModels) {
		String uSubStartTime = commonUserSubjectModel.uSubStartTime;
		Long valueOf = Long.valueOf(uSubStartTime);
		String formatTime = DataUtils.getInstance().formatTime(valueOf);
		commonUserSubjectModel.formatTime = formatTime;
		
	    }

	    responeModel.code = "1000";
	    responeModel.message="查询成功";
	    responeModel.content = queryAllTestModels;

	} catch (Exception e) {

	    responeModel.code = "1001";
	    responeModel.message="异常"+e.getMessage();

	}


	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[]{"itemSubmainSub"});
	String string = JSONSerializer.toJSON(responeModel,config).toString();

	return string;
    }


    //完成提交 
    public String finishFunction(HttpServletRequest request) {

	String userid = request.getParameter("useruid");
	String subData= request.getParameter("datas");
	String userName = request.getParameter("userName");


	System.out.println(userid);
	System.out.println(subData);
	System.out.println(userName);


	ResponeModel<Object> responeModel = new ResponeModel<>();
	String resultString = "";

	//查询用户信息
	UserModel queryModel = null;
	if (userid!=null&&!userid.equals("")) {
	    queryModel = userDao.queryModelFormUid(userid);
	}

	if (queryModel==null) {
	    if (userName!=null&&!userName.equals("")) {
		queryModel = userDao.queryModelFormName(userName);
	    }
	}


	if (queryModel==null) {
	    responeModel.code="1002";
	    responeModel.message="无用户信息 ";
	} else {
	    try {
		System.out.println("开始解析 ... ...");
		UserSubjectModel cModel = JSON.parseObject(subData, UserSubjectModel.class);

		UserSubjectModel userSubjectModel = new UserSubjectModel();
		//图标 
		userSubjectModel.uSubImagUrl = cModel.uSubImagUrl;
		userSubjectModel.uSubName = cModel.uSubName;
		userSubjectModel.uSubPreface = cModel.uSubPreface;
		userSubjectModel.uSubSelectCount = cModel.uSubSelectCount;
		userSubjectModel.uSubStartTime = cModel.uSubStartTime;
		System.out.println("--- --- ---"+cModel.uSubUid);
		userSubjectModel.uSubUid=cModel.uSubUid;
		userSubjectModel.uSubUseTime = cModel.uSubUseTime;
		userSubjectModel.uSubWriteCount = cModel.uSubWriteCount;
		userSubjectModel.uSubUserFlag = cModel.uSubUserFlag;
		userSubjectModel.uSubUserId = cModel.uSubUserId;

		Set<UserItemSubjectModel> uSubItemList = cModel.uSubItemList;
		for (UserItemSubjectModel uItemModel : uSubItemList) {
		    UserItemSubjectModel userItemSubjectModel = new UserItemSubjectModel();

		    userItemSubjectModel.itemSubCount = uItemModel.itemSubCount;
		    userItemSubjectModel.itemSubDetaile = uItemModel.itemSubDetaile;

		    userItemSubjectModel.itemSubNormalSelect = uItemModel.itemSubNormalSelect;
		    userItemSubjectModel.itemSubSelectList = uItemModel.itemSubSelectList;

		    userItemSubjectModel.itemSubTitle = uItemModel.itemSubTitle;
		    userItemSubjectModel.itemSubType = uItemModel.itemSubType;

		    userItemSubjectModel.itemSubUselect = uItemModel.itemSubUselect;
		    userItemSubjectModel.itemUuid = uItemModel.itemUuid;


		    userItemSubjectModel.itemSubmainSub = userSubjectModel;
		    userSubjectModel.uSubItemList.add(userItemSubjectModel);
		}


		//保存
		Session currentSession = H3Utils.getCurrentSession();
		queryModel.getUserSubjectList().add(userSubjectModel.uSubUid);
		System.out.println(""+queryModel.getUserSubjectList());
		currentSession.saveOrUpdate(queryModel);
		currentSession.saveOrUpdate(userSubjectModel);






		System.out.println("操作成功");
		responeModel.code="1000";
		responeModel.message="操作成功  ";

	    } catch (Exception e) {
		System.out.println("保存 失败 "+e.getMessage());
	    }

	}

	try {
	    resultString = JSONSerializer.toJSON(responeModel).toString();
	} catch (Exception e) {
	    // TODO: handle exception
	}

	return resultString;
    }

    //添加试题
    public String addSubFuntion(HttpServletRequest request) {

	String subTitle = request.getParameter("subTitle");
	String subSgine = request.getParameter("subSgine");
	System.out.println(" "+ subSgine +" \t "+subTitle);

	ResponeModel<UserSubjectModel> responeModel = new ResponeModel<>();


	if (subTitle==null||subTitle.equals("")||subSgine==null||subSgine.equals("")) {
	    responeModel.code="1002";
	    responeModel.message="名称或 题记为空 ";

	}else {

	    if (subTitle!=null&&!subTitle.equals("")) {
		subTitle =URLDecoder.decode(subTitle);
	    }
	    if (subSgine!=null&&!subSgine.equals("")) {
		subSgine =URLDecoder.decode(subSgine);
	    }

	    CommonUserSubjectModel userSubjectModel = new CommonUserSubjectModel();
	    userSubjectModel.uSubName = subTitle;
	    userSubjectModel.uSubPreface = subSgine;

	    userSubjectModel.uSubStartTime = System.currentTimeMillis()+"";
	    userSubjectModel.uSubUserFlag = 0;
	    userSubjectModel.uSubUid = UUID.randomUUID().toString();

	    dao.addTestModel(userSubjectModel);

	    responeModel.code="1000";
	    responeModel.message="操作成功 ";
	}

	String json = GsonUtils.getintance().toJson(responeModel, null);
	System.out.println(json);
	
	return json;
    }



}

